package com.by.yz.activity.regitser;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.by.yz.App;
import com.by.yz.R;
import com.by.yz.Register.RegisterRESTFul;
import com.by.yz.activity.LoginActivity;
import com.by.yz.activity.MainActivity;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.chatuidemo.DemoHelper;
import com.by.yz.common.MFGT;
import com.by.yz.entity.Account;
import com.by.yz.entity.Register;
import com.by.yz.entity.UserTest;
import com.by.yz.loginUtil.AccountRESTFul;
import com.by.yz.loginUtil.UserRESTFul;
import com.by.yz.util.ToJSonObject;
import com.hyphenate.EMCallBack;
import com.hyphenate.EMError;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by John on 2017/3/18.
 */
public class RegisterInfoNextActivity  extends BaseActivity implements View.OnClickListener{
    private EditText registerEduction;
    private EditText registerEmail;
    private EditText registerUserresume;
    private EditText registerHistory;
    private Button finish,ignore;
    private UserTest user;
    private Account account;
    private  String eduction,email,userresume,history,number,password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registerinfo_next);
        registerEduction=(EditText)findViewById(R.id.eduction);
        registerEmail=(EditText)findViewById(R.id.email);
        registerHistory=(EditText)findViewById(R.id.history);
        registerUserresume=(EditText)findViewById(R.id.userresume);
        user=new UserTest();
        final Intent intent = getIntent();
        number = intent.getStringExtra("number").toString();
        //跳过
        ignore=(Button)findViewById(R.id.ignore_next);
        ignore.setOnClickListener(this);
//        ignore.setOnClickListener(new View.OnClickListener(){
//
//            @Override
//            public void onClick(View view) {
//                intent.setClass(RegisterInfoNextActivity.this,MainActivity.class);
//                startActivity(intent);
//                finish();
//            }
//        });
        //完成
        finish = (Button) findViewById(R.id.register_next_finish);
        finish.setOnClickListener(this);
//        finish.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                AccountRESTFul accountRequste = new AccountRESTFul();
//                accountRequste.get( new AccountRESTFul.IhttpCb() {
//                    @Override
//                    public void ok(JSONObject response) {
//                        //JSONObject jsonObject=response;
//                        //account=ToJSonObject.jsonToBean(jsonObject.toString(),Account.class);
//                        Log.e("hahahahh``","授权"+response);
//                        eduction=registerEduction.getText().toString();
//                        history=registerHistory.getText().toString();
//                        email=registerEmail.getText().toString();
//                        userresume=registerUserresume.getText().toString();
//                        getUser();
//                        //Register register = (Register)intent.getSerializableExtra("register");
//
//                        //number=register.getNumber();
//                        //password=register.getPassword();
//                        Log.e("qwq", "number"+number);
//
//
//
//                       // updateUser();
//                    }
//                    @Override
//                    public void error(String error) {
//                        // 授权失败
//                        Log.e("GG", "授权失败");
//                    }
//                });
//
//            }
//        });
    }
    @Override
    public void onClick(View v) {
        Intent intent =new Intent();
        switch (v.getId()) {
            case R.id.ignore_next:
//                intent.setClass(RegisterInfoNextActivity.this,LoginActivity.class);
//                startActivity(intent);
//                Handler handler = new Handler();
//                handler.postDelayed(new Runnable() {
//                    public void run() {
//                        MFGT.finish(RegisterInfoNextActivity.this);
//                    }
//                },2000);
                out();
                break;
            case R.id.register_next_finish:
                finishRegister();
                out();
                break;
        }
    }
    public void finishRegister() {
        AccountRESTFul accountRequste = new AccountRESTFul();
        accountRequste.get( new AccountRESTFul.IhttpCb() {
            @Override
            public void ok(JSONObject response) {
                //JSONObject jsonObject=response;
                //account=ToJSonObject.jsonToBean(jsonObject.toString(),Account.class);
                Log.e("hahahahh``","授权"+response);
                eduction=registerEduction.getText().toString();
                history=registerHistory.getText().toString();
                email=registerEmail.getText().toString();
                userresume=registerUserresume.getText().toString();
                getUser();
                //Register register = (Register)intent.getSerializableExtra("register");

                //number=register.getNumber();
                //password=register.getPassword();
                Log.e("qwq", "number"+number);



                // updateUser();
            }
            @Override
            public void error(String error) {
                // 授权失败
                Log.e("GG", "授权失败");
            }
        });

    }
    public void out(){
        final ProgressDialog pd = new ProgressDialog(RegisterInfoNextActivity.this);
        String st = getResources().getString(R.string.Are_logged_out);
        pd.setMessage(st);
        pd.setCanceledOnTouchOutside(false);
        pd.show();
        EMClient.getInstance().logout(true,new EMCallBack() {
            @Override
            public void onSuccess() {
                RegisterInfoNextActivity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        pd.dismiss();
                    }
                });
            }
            @Override
            public void onProgress(int progress, String status) {
            }
            @Override
            public void onError(int code, String message) {
                RegisterInfoNextActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        pd.dismiss();
                        Toast.makeText(RegisterInfoNextActivity.this, "unbind devicetokens failed", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        setResult(RESULT_OK);
        //清除登陆缓存token
        App.clearCacheUserToken(RegisterInfoNextActivity.this);
        Intent intent = new Intent(RegisterInfoNextActivity.this, LoginActivity.class);
        intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }
    public void getUser(){
        //获取用户信息
        UserRESTFul userRESTFul=new UserRESTFul();
        userRESTFul.getUser(number, new UserRESTFul.UserPOSTCallBack() {
            @Override
            public void ok(JSONObject response) throws JSONException {
                JSONObject jsonObject=response;
                Log.e("hahahahh``","获取用户表成功"+response);
                user=ToJSonObject.jsonToBean(jsonObject.toString(),UserTest.class);
                user.setEmail(email);
                user.setUserresume(userresume);
                user.setEducation(eduction);
                user.setHistory(history);
                updateUser();
            }
            @Override
            public void error(String response) {
                Log.e("hahahahh","获取用户表失败"+response);
            }
        });
    }

    public void updateUser(){
        JSONObject data = ToJSonObject.beanToJSONObejct(user);
        UserRESTFul userRESTFul=new UserRESTFul();
        userRESTFul.putUser(data, new UserRESTFul.UserPOSTCallBack() {
            @Override
            public void ok(JSONObject response) throws JSONException {
                Intent intent=new Intent();
                Toast.makeText(getApplicationContext(), "信息完善成功", Toast.LENGTH_SHORT).show();
//                intent.setClass(RegisterInfoNextActivity.this,LoginActivity.class);
//                startActivity(intent);
//                Handler handler = new Handler();
//                handler.postDelayed(new Runnable() {
//                    public void run() {
//                        MFGT.finish(RegisterInfoNextActivity.this);
//                    }
//                },2000);
            }
            @Override
            public void error(String response) {
                Toast.makeText(getApplicationContext(), "信息完善失败，请检查网络", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
